package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.base.util.MD5Util;
import com.imay.live.activity.user.ResigterActivity;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.helper.PreferenceHelper;
import com.imay.live.manager.AccountManager;
import com.imay.live.manager.EditTextManager;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.ResultNoData;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.utils.ToastUtil;
import com.imay.live.R;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.helper.PreferenceHelper.SECRET;
import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * create by nick
 * 我的->设置->账号安全->修改密码
 */
public class PasswordResetActivity extends BaseActivity implements View.OnClickListener {


    public static void start(Context context) {
        Intent starter = new Intent(context, PasswordResetActivity.class);
        context.startActivity(starter);
    }

    private EditText mCurrentPasswordET, mNewPasswordET, mConfirmPasswordET;
    private Button mConfirmBtn;
    private String phone;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_password_reset;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        setTitle(getString(R.string.setting_reset_password));

        mCurrentPasswordET = $(R.id.et_current_psw);
        mNewPasswordET = $(R.id.et_new_psw);
        mConfirmPasswordET = $(R.id.et_confirm_psw);
        mConfirmBtn = $(R.id.btn_confirm);

        $(R.id.tv_forgot_psw).setOnClickListener(this);
        mConfirmBtn.setOnClickListener(this);

        phone = PreferenceHelper.getInstance().getPhone();
        //List<EditText> editTexts = new ArrayList<>();
        mNewPasswordET.setTag("psd");
        mConfirmPasswordET.setTag("psd");
        EditTextManager.getInstance().changeBgColor(mConfirmBtn, R.drawable.shape_corner100_bc, R.drawable.shape_corner100_9e, mCurrentPasswordET, mNewPasswordET, mConfirmPasswordET);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                if (!mNewPasswordET.getText().toString().equals(mConfirmPasswordET.getText().toString())) {
                    ToastUtil.show(this, R.string.password_not_same);
                    return;
                }
                changePassword();
                break;
            case R.id.tv_forgot_psw:
                ResigterActivity.startActivity(this, ResigterActivity.STATUS_FORGOT_PASSWORD_NO_LOGIN);
                break;
        }

    }

    private void changePassword() {
        AccountManager.getInstance().changePassword(phone, mCurrentPasswordET.getText().toString(), mNewPasswordET.getText().toString(), new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                ResultNoData result = JSON.parseObject(data, new TypeReference<ResultNoData>() {
                }.getType());
                if (result.result == CodeSuccess) {
//                    AccountManager.getInstance().logout();
//                    PreferenceHelper.getInstance().clearSec();
//                    LoginActivity.startActivityForLogout(PasswordResetActivity.this);
                    String phone = UserInfoCache.getUserInfo().getPhone() + "";
                    String psw = mNewPasswordET.getText().toString();
                    PreferenceHelper.getInstance().putString(SECRET, MD5Util.getMD5(phone + psw));
                    ToastUtil.show(PasswordResetActivity.this, R.string.change_password_success);

                    finish();
                } else {
                    ErrorResult.toastError(PasswordResetActivity.this, result.result);
                }

            }

            @Override
            public void onRequestError(String command, int code, String message) {
                ErrorResult.toastError(PasswordResetActivity.this, code);
            }
        });
    }

}
